package cn.lzj.user.service.impl;

import cn.lzj.basic.constants.VerifyCodeConstants;
import cn.lzj.basic.exception.BusinessException;
import cn.lzj.basic.service.impl.BaseServiceImpl;
import cn.lzj.basic.util.MD5Utils;
import cn.lzj.basic.util.StrUtils;
import cn.lzj.user.domain.Logininfo;
import cn.lzj.user.domain.User;
import cn.lzj.user.dto.UserDto;
import cn.lzj.user.mapper.LogininfoMapper;
import cn.lzj.user.mapper.UserMapper;
import cn.lzj.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {


    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void phoneregister(UserDto userDto) {
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        if(StringUtils.isEmpty(phone)||
           StringUtils.isEmpty(phoneCode)||
           StringUtils.isEmpty(password)||
           StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("请输入正确的数据!!!");
        }
        //校验两次密码是否一样
        if(!password.trim().equals(passwordRepeat.trim())){
            throw new BusinessException("两次密码不一致!!!");
        }
        Object redisphone = redisTemplate.opsForValue().get(VerifyCodeConstants.REGISTER_CODE_PREFIX + phone);
        if(redisphone==null){
            throw new BusinessException("验证码无效,请重新获取!!!");
        }
        //判断验证码是否正确
        String codeTmp = redisphone.toString().split(":")[0];
        if(!codeTmp.equalsIgnoreCase(phoneCode)){
            throw new BusinessException("验证码错误!!!");
        }
        //2.保存数据
        //2.1先保存t_logininfo
        User user=userDto2User(userDto);
        Logininfo logininfo=userDto2Logininfo(user);
        logininfoMapper.save(logininfo);
        //设置登陆信息id
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }
    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(md5password);
        return user;
    }
    private Logininfo userDto2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }
    @Override
    public void emailregister(UserDto userDto) throws MessagingException {
        String email=userDto.getEmail();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        if( StringUtils.isEmpty(email)||
            StringUtils.isEmpty(password)||
            StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("请输入正确的数据!!!");
        }//校验
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致!!!");
        }//添加数据
        //添加邮箱数据到logininfo//加密加盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        Logininfo logininfo = new Logininfo();
        logininfo.setType(0);
        logininfo.setSalt(salt);
        logininfo.setEmail(email);
        logininfo.setPassword(md5Pwd);
        logininfoMapper.save(logininfo);
        //3.发送邮件：审核通过邮件 - 叫他激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("2199829746@qq.com");
        //设置主题
        helper.setSubject("店铺注册邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<a href='http://127.0.0.1/login.html'>点击该链接登录!!!</a>",true);
        //设置收件人
        //Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("2199829746@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }
}
